﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml;

namespace WPKeyboard
{

    
    public class KeyInfo
    {
        public string Value { get; set; }
        public string Caps { get; set; }
    }

    public enum LanguageTextDirection
    {
        LeftToRight,
        RightToLeft
    }

    public class LanguageProperties
    {
        public string Caption { get; set; }
        public LanguageTextDirection TextDirection { get; set; }
    }


    public static class KeyboardInfoUtilities
    {
        static KeyboardInfoUtilities()
        {
            LoadLanguages();
            LoadSpecialCharacters();
        }

        public static void LoadLanguages()
        {
                XDocument doc = XDocument.Load(@"xml\languages.xml");
                foreach (XElement xe2 in doc.Element("LanguageDetails").Elements())
                {
                    string key = xe2.Attribute("key").Value;
                    Languages.Add(key, new Dictionary<string, KeyInfo>());
                    
                    // Setting text direction (LeftToRight or RightToLeft - LTR is the default if not set
                    LanguagesProperties.Add(key, new LanguageProperties() { TextDirection = (xe2.Attribute("direction") == null || xe2.Attribute("direction").Value == "LTR") ? LanguageTextDirection.LeftToRight : LanguageTextDirection.RightToLeft,
                                                                            Caption = xe2.Attribute("caption").Value});

                    foreach(XElement xe3 in xe2.Elements())
                        Languages[key].Add(xe3.Attribute("ID").Value, new KeyInfo() { Value = xe3.Attribute("Value").Value, Caps = xe3.Attribute("Caps").Value });
                }
        }

        public static void LoadSpecialCharacters()
        {
            XDocument doc = XDocument.Load(@"xml\specialCharacters.xml");
            foreach (XElement xe2 in doc.Element("SpecialCharacters").Elements())
            {
                SpecialCharacters.Add(xe2.Attribute("ID").Value, new KeyInfo()
                {
                    Value = xe2.Attribute("Value").Value,
                    Caps = xe2.Attribute("Caps").Value
                });
            }
        }

        public static List<string> GetCheckedLanguagesKeys()
        {
            List<string> SelectedLanguagesKeys = new List<string>();

            foreach (string key in GetCheckedLanguages().Keys)
            {
                SelectedLanguagesKeys.Add(key);
            }

            return SelectedLanguagesKeys;
        }


        public static Dictionary<string, Dictionary<string, KeyInfo>> GetCheckedLanguages()
        {
            Dictionary<string, Dictionary<string, KeyInfo>> SelectedLanguages = new Dictionary<string, Dictionary<string, KeyInfo>>();

            AppSettings settings = new AppSettings();
            foreach (string key in LanguagesProperties.Keys)
	        {
                // for each language, check if it checked, if so, add it to the list
                //TODO: move to global const
                if (settings.GetOtherKeyboardChecked(LanguagesProperties[key].Caption + AppSettings.constKeyboardChecked))
                {
                    SelectedLanguages.Add(key, Languages[key]);
                }
	        }
            return SelectedLanguages;
        }

        public static Dictionary<string, KeyInfo> SpecialCharacters = new Dictionary<string, KeyInfo>();
        public static Dictionary<string, Dictionary<string, KeyInfo>> Languages = new Dictionary<string, Dictionary<string, KeyInfo>>();
        public static Dictionary<string, LanguageProperties> LanguagesProperties = new Dictionary<string, LanguageProperties>();
    }
}
